﻿using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using ResumeSearch.Properties;

namespace ResumeSearch
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// please note that this code is being distributed 
        /// under MIT Liscence by www.ahrcloud.com. We 
        /// encourage attribution of our open source work 
        /// and lab experiements & we expect the same from you!
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        private void ExitApp()
        {
            GC.Collect();
            Application.Exit();
        }

        private void ButtonExit_Click(object sender, EventArgs e)
        {
            ExitApp();
        }

        private void ButtonStart_Click(object sender, EventArgs e)
        {
            #region parseresume

            //create datalayer ref.
            var _datah = new Cldatahandler();

            //check if the path is not empty

            if (textBox1.Text != "")
            {
                //convert from word to text
                var _process = new Clprocessor();

                string _tempstore = _process.Convertwordtotext(textBox1.Text.Trim()).ToLowerInvariant();

                //process text
                string[] _splitoptions = { "\n", "\r", "\a", "\t" };
                string[] _mainarray = _tempstore.Split(_splitoptions, StringSplitOptions.RemoveEmptyEntries);

                //marker legend
                //index 0 = Profile at index
                //index 1 = Education at index
                //index 3 = Experience at index
                //index 4 = Skills at index

                var _markers = new int[4];

                //these flags below are necessary to avoid next item
                //and to save us from looping into for again and 
                //again.

                int[] flags = { 0, 0, 0, 0 };

                //first we find sections.
                for (int i = 0; i < _mainarray.Length; i++)
                {
                    if (_mainarray[i].Contains("summary") || _mainarray[i].Contains("goal") ||
                        _mainarray[i].Contains("objective") || _mainarray[i].Contains("profile"))
                    {
                        if (flags[0] == 0)
                        {
                            //found profile
                            _markers[0] = i;
                            flags[0] = 1;
                        }
                    }

                    else if (_mainarray[i].Contains("education") || _mainarray[i].Contains("schooling") ||
                             _mainarray[i].Contains("educational"))
                    {
                        if (flags[1] == 0)
                        {
                            //found education
                            _markers[1] = i;
                            flags[1] = 1;
                        }
                    }

                    else if (_mainarray[i].Contains("experience") || _mainarray[i].Contains("accomplishment"))
                    {
                        if (flags[2] == 0)
                        {
                            //found experience
                            _markers[2] = i;
                            flags[2] = 1;
                        }
                    }

                    else if (_mainarray[i].Contains("skill") || _mainarray[i].Contains("competencies"))
                    {
                        if (flags[3] == 0)
                        {
                            //found skills
                            _markers[3] = i;
                            flags[3] = 1;
                        }
                    }

                    else
                    {
                        //non standard resume
                    }
                }

                //sort them
                Array.Sort(_markers);

                var _output = new StringBuilder();
                var _mark = "";

                if (_mainarray.Length > 4)
                {
                    _mark += _mainarray[_markers[0]];
                    _output.Append(_mark);

                    string maxid = _datah.GetGuid();
                    int j = 0;

                    for (j = _markers[0]; j < _markers[1]; j++)
                    {
                        if (_mark != "")
                        {
                            _output.Append(_mainarray[j]);
                            _datah.InsertData(maxid, _mark, _mainarray[j]);
                        }
                    }

                    _mark = "";
                    _mark += _mainarray[_markers[1]];
                    _output.Append(_mark);

                    for (j = _markers[1]; j < _markers[2]; j++)
                    {
                        if (_mark != "")
                        {
                            _output.Append(_mainarray[j]);
                            _datah.InsertData(maxid, _mark, _mainarray[j]);
                        }
                    }

                    _mark = "";
                    _mark += _mainarray[_markers[2]];
                    _output.Append(_mark);

                    for (j = _markers[2]; j < _markers[3]; j++)
                    {
                        if (_mark != "")
                        {
                            _output.Append(_mainarray[j]);
                            _datah.InsertData(maxid, _mark, _mainarray[j]);
                        }
                    }

                    _mark = "";
                    _mark += _mainarray[_markers[3]];
                    _output.Append(_mark);

                    for (j = _markers[3]; j < _mainarray.Length; j++)
                    {
                        if (_mark != "")
                        {
                            _output.Append(_mainarray[j]);
                            _datah.InsertData(maxid, _mark, _mainarray[j]);
                        }
                    }
                }

                var _rset = _output.ToString();

                GC.Collect();

                MessageBox.Show(_rset == "" ? Resources.Res_Nonstandard_Resume : _output.ToString());
            }

            else
            {
                //need file path
                MessageBox.Show(Resources.Res_Empty_FileName);
            }

            #endregion
        }

        private void ButtonUpload_Click(object sender, EventArgs e)
        {
            openfiled.ShowDialog();
        }

        private void openfiled_FileOk(object sender, CancelEventArgs e)
        {
            textBox1.Text = openfiled.FileName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            #region refreshgrid
            
            dataGridView1.Rows.Clear();

            var _datah = new Cldatahandler();
            var _alist = _datah.GetData(TextBoxSearch.Text.Trim());
            int j = 0;

            dataGridView1.Rows.Add(_alist.Count/2);

            for (int i = 0; i < _alist.Count; i += 2)
            {
                dataGridView1.Rows[j].Cells[0].Value = _alist[i];
                dataGridView1.Rows[j].Cells[1].Value = _alist[i + 1];
                j++;
            }
            
            #endregion
        }
    }
}